﻿using _12306helper.Common;
using _12306helper.Entity;
using _12306helper.Service;
using _12306helper.UI.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _12306helper.UI
{
    public partial class MainForm : Form
    {
        public delegate void ColorTextHandler(RichTextBox box, string msg, Color color);
        public delegate void CheckedListBoxHandle(CheckedListBox box, object datasource, string display, string value);
        public bool isLogin = false;
        public MainForm()
        {
            InitializeComponent();
            isLogin = false;
            LoginInit();
            InitDate();
            bwInit.RunWorkerAsync();
        }
        private void InitDate()
        {
            DateTime maxDate = DateTime.Now.AddDays(Double.Parse(Resources.saleDays));
            dtpDate.Value = maxDate;
            dtpDate.MaxDate = maxDate;
            dtpDate.MinDate = DateTime.Now;
        }
        public void MsgInfo(string msg)
        {
            ColorText(rtxConsole, msg, Color.Green);
        }
        public void MsgError(string msg)
        {
            ColorText(rtxConsole, msg, Color.Red);
        }
        private void FillDSToCheckListBox(CheckedListBox box, object datasource, string display, string value)
        {
            box.DataSource = datasource;
            box.ValueMember = value;
            box.DisplayMember = display;
        }
        private void setColorText(RichTextBox box, string msg, Color color)
        {
            box.AppendText(Environment.NewLine);
            string text = string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss:fff"), msg);
            box.AppendText(text);
            int begin = box.TextLength - text.Length;
            box.Select(begin, text.Length);
            box.SelectionColor = color;
            box.SelectionStart = box.TextLength;
        }
        private void ColorText(RichTextBox box, string msg, Color color)
        {
            Invoke(new ColorTextHandler(setColorText), box, msg, color);
        }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            MsgInfo("你好啊");
            MsgError("你好啊2");
        }

        private void tsmiLogin_Click(object sender, EventArgs e)
        {
            MsgInfo("开始登录");
            LoginForm loginForm = new LoginForm();
            DialogResult dr = loginForm.ShowDialog();
            if (dr == DialogResult.Cancel)
            {
                MsgInfo("用户取消了登录");
            }
            else if (dr == DialogResult.OK)
            {
                MsgError("用户成功登录");
                isLogin = true;
                LoginInit();
                //取联系人信息
                if (!bwPassengers.IsBusy) bwPassengers.RunWorkerAsync();
            }
            else
            {
                MsgError("登录异常");
            }
        }
        private void LoginInit()
        {
            if (isLogin)
            {
                tsslLogin.Text = "已登录";
                tsslLogin.ForeColor = Color.Green;
                tsmiLogin.Visible = false;
                tsmiLogout.Visible = true;
            }
            else
            {
                tsslLogin.Text = "未登录";
                tsslLogin.ForeColor = Color.Red;
                tsmiLogin.Visible = true;
                tsmiLogout.Visible = false;
            }
        }
        private void tsmiQuit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定退出吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                System.Environment.Exit(0);
            }
        }

        private void bwInit_DoWork(object sender, DoWorkEventArgs e)
        {
            MsgInfo("初始化资源……");
            InitSeats();
            InitStation();
        }
        private void InitSeats()
        {
            CommonUtil.Seats = JsonHelper.FromJson<List<Seat>>(Resources.Seats);
            Invoke(new CheckedListBoxHandle(FillDSToCheckListBox), clbSeat, CommonUtil.Seats, "Text", "Code");
        }
        private void InitStation()
        {
            CommonUtil.Stations = JsonHelper.FromJson<List<Station>>(Resources.Stations);
        }
        private void bwPassengers_DoWork(object sender, DoWorkEventArgs e)
        {
            MsgInfo("开始获取常用联系人列表");
            List<Passenger> data = UserService.GetPassengers();
            if (data != null && data.Count > 0)
            {
                CommonUtil.Passengers = data;
                Invoke(new CheckedListBoxHandle(FillDSToCheckListBox), clbPassenger, data, "passenger_name", "code");
            }
        }

        private void lnlAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            TrainQueryForm train = new TrainQueryForm();
            train.ShowDialog();
        }

    }
}
